﻿using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iShoppingBUS;
using iShoppingDTO;
using System.Text;

namespace iShopping.Guest
{
    public partial class LoginPage : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            GiaoDienDTO giaoDienDto = GiaoDienBUS.SelectGiaoDienMa("Theme001");
            if (giaoDienDto != null)
            {
                this.Theme = giaoDienDto.TenTheme;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            scriptManager.SetFocus(bt_rlogin);
        }

        protected void btnRlogin_Click(object sender, EventArgs e)
        {
            TaiKhoanDTO tk = new TaiKhoanDTO();
            tk.TenTaiKhoan = _acc.Value;
            tk.MatKhau = _pass.Value;
            bool result = TaiKhoanBUS.checkTaiKhoan(tk);
            string maTaiKhoan = TaiKhoanBUS.GetTaiKhoanOfTens(tk.TenTaiKhoan);
            string maLoaiTK = TaiKhoanBUS.LayMaLoaiTK(tk.TenTaiKhoan); //dung
            if (result == true && maLoaiTK == "2")
            {
                Session.Add("username", tk.TenTaiKhoan);
                Session.Add("userid", maTaiKhoan);
                if (Session["previousUri"] != null)
                {
                    string preUri = (string)Session["previousUri"];
                    Session.Remove("previousUri");
                    //preUri = Regex.Replace(preUri, "/Guest/", "/Member/");
                    //Response.Redirect(preUri);
                    Response.Redirect("~/Member/HomePage.aspx"); //dung
                }
                else
                {
                    Response.Redirect("/Member/HomePage.aspx");
                }
            }

            else if (result == true && maLoaiTK == "3")
            {
                Session.Add("username", tk.TenTaiKhoan);
                Session.Add("userid", maTaiKhoan);
                if (Session["previousUri"] != null)
                {
                    string preUri = (string)Session["previousUri"];
                    Session.Remove("previousUri");
                    //preUri = Regex.Replace(preUri, "/Guest/", "/Member/");
                    //Response.Redirect(preUri);
                    Response.Redirect("~/Manager/HomePage.aspx"); //dung
                }
                else
                {
                    Response.Redirect("/Manager/HomePage.aspx");
                }
            }
            else if (result == true && maLoaiTK == "4")
            {
                Session.Add("username", tk.TenTaiKhoan);
                Session.Add("userid", maTaiKhoan);
                if (Session["previousUri"] != null)
                {
                    string preUri = (string)Session["previousUri"];
                    Session.Remove("previousUri");
                    //preUri = Regex.Replace(preUri, "/Guest/", "/Member/");
                    //Response.Redirect(preUri);
                    Response.Redirect("~/Admin/QuanLyTaiKhoan.aspx"); //dung
                }
                else
                {
                    Response.Redirect("~/Admin/QuanLyTaiKhoan.aspx");
                }
            }
            else
            {
                lb_Err.Text = "Sai tên đăng nhập hoặc mật khẩu";
            }
        }
    }
}
